package me.sealer.ssm.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import me.sealer.ssm.constant.PageConstant;
import me.sealer.ssm.constant.RoleConstant;
import me.sealer.ssm.controller.dto.CommonResponse;
import me.sealer.ssm.controller.dto.DeleteMenuRequest;
import me.sealer.ssm.controller.dto.NewMenuRequest;
import me.sealer.ssm.model.Menu;
import me.sealer.ssm.page.PageResult;
import me.sealer.ssm.service.MenuService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @author sealer
 * @date 2016/09/27
 */
@Slf4j
@RestController
@Api(tags = "菜单管理相关api")
@RequestMapping("/menuOperations")
@RequiresRoles(RoleConstant.ROLE_ADMIN)
public class MenuController {
    private final MenuService menuService;

    public MenuController(MenuService menuService) {
        this.menuService = menuService;
    }

    @ApiOperation(value = "根据条件分页查询菜单列表")
    @GetMapping(value = "/queryMenusPage", produces = MediaType.APPLICATION_JSON_VALUE)
    public CommonResponse<PageResult<Menu>> queryMenusPage(
            @ApiParam(value = "菜单别名") @RequestParam(value = "alias", required = false) String alias,
            @ApiParam(value = "菜单类型") @RequestParam(value = "type", required = false) String type,
            @ApiParam(value = "菜单级别") @RequestParam(value = "level", required = false) Integer level,
            @ApiParam(value = "第几页", required = true) @RequestParam(value = PageConstant.PAGE_NO) Integer pageNo,
            @ApiParam(value = "每页记录条数", required = true) @RequestParam(value = PageConstant.PAGE_SIZE) Integer pageSize) {

        //调用service方法得到菜单列表
        PageResult<Menu> pageResult = menuService.queryMenusPage(alias, type, level, pageNo, pageSize);
        return new CommonResponse<>(pageResult);
    }

    @ApiOperation(value = "查询菜单列表")
    @GetMapping(value = "/queryMenus", produces = MediaType.APPLICATION_JSON_VALUE)
    public CommonResponse<List<Menu>> queryMenus() {

        //调用service方法得到菜单列表
        List<Menu> menus = menuService.queryAllMenus();
        return new CommonResponse<>(menus);
    }

    @ApiOperation(value = "查询所有一级菜单列表")
    @GetMapping(value = "/queryFirstMenus", produces = MediaType.APPLICATION_JSON_VALUE)
    public CommonResponse<List<Menu>> queryFirstMenus() {

        //调用service方法得到菜单列表
        List<Menu> menus = menuService.queryFirstMenus();
        return new CommonResponse<>(menus);
    }

    @ApiOperation(value = "根据条件查询菜单列表")
    @GetMapping(value = "/queryParentMenusByLevel", produces = MediaType.APPLICATION_JSON_VALUE)
    public CommonResponse<List<Menu>> queryParentMenusByLevel(
            @ApiParam(value = "菜单级别", required = true) @RequestParam(value = "level") Integer level) {

        // 一共只有四级菜单，只有2、3、4级菜单有父级菜单， 若不合法， 则返回empty list
        boolean levelValid = level >= 2 && level <= 4;
        if (!levelValid) {
            return new CommonResponse<>(new ArrayList<>());
        }

        Integer parentLevel = level - 1;
        List<Menu> menus = menuService.queryMenus(null, null, parentLevel);
        return new CommonResponse<>(menus);
    }

    @ApiOperation(value = "新增菜单")
    @PostMapping(value = "/newMenu", produces = MediaType.APPLICATION_JSON_VALUE)
    public CommonResponse newMenu(@Valid @RequestBody NewMenuRequest newMenuRequest) {

        Menu menu = Menu.builder()
                .alias(newMenuRequest.getAlias())
                .description(newMenuRequest.getDescription())
                .icon(newMenuRequest.getIcon())
                .name(newMenuRequest.getName())
                .type(newMenuRequest.getType())
                .parentMenuId(newMenuRequest.getParentMenuId())
                .build();
        menuService.insertOne(menu);
        return new CommonResponse();
    }

    @ApiOperation(value = "删除菜单")
    @PostMapping(value = "/deleteMenu", produces = MediaType.APPLICATION_JSON_VALUE)
    public CommonResponse deleteMenu(@Valid @RequestBody DeleteMenuRequest deleteMenuRequest) {

        menuService.deleteMenu(deleteMenuRequest.getId(), deleteMenuRequest.getAlias(), deleteMenuRequest.getType());
        return new CommonResponse();
    }
}
